<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car\CarCost;
use Illuminate\Http\Request;
use Modules\Car\Entities\RecognizeDrivingLicense;

class CostController extends Controller
{
    use ResponseData;
    public function index(CarCost $model,Request $request) {
        $size = $request->input('size',10);
        $data = $model->paginate($size);
        return $this->success($data);
    }
    public function all(CarCost $model) {
        $data = $model->with('car')->get();
        return $this->success($data);
    }
    public function store(CarCost $model,Request $request){
        $data = $request->all();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');

    }
    public function edit(CarCost $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
    public function update(CarCost $model,Request $request){
        $data = $model->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function destory(CarCost $model,Request $request){
        $data = $model->find($request->id);
        $data->delete();
    }
    public function xiufu(CarCost $model,Request $request){
        $data = $model->get()->toArray();
        $conts = count($data);
        for ($o=0; $o < $conts; $o++) {
            $cardetail = RecognizeDrivingLicense::where('id', $data[$o]['car_id'])->first();
            if($cardetail){
                $model->where('id', $data[$o]['id'])->update(['plate_number'=>$cardetail->plate_number]);
            }else{
                $deldetail = RecognizeDrivingLicense::onlyTrashed()->where('id', $data[$o]['car_id'])->first();
                $model->where('id', $data[$o]['id'])->update(['plate_number'=>$deldetail->plate_number]);
            }
        }
        return $this->success('修复成功');
    }
}
